package cn.e4j.modular.api.service;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.modular.api.mapper.XchbMapper;
import cn.e4j.modular.crm.entity.*;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenchuan on 2018/4/16.
 * <p>
 * 现场回报
 */

@Service
public class XchbService {

    @Autowired
    private XchbMapper xchbMapper;

    /**
     * 获取现在回报单列表
     *
     * @param inDto
     * @return
     */
    public List<Dto> getListPage(Page<Object> page, Dto inDto) {
        List<Dto> list = xchbMapper.getListPage(page,inDto);
        for(int i = 0; i < list.size(); i ++){
            if(list.get(i).getString("MMRD013").indexOf(",") != -1){
                Dto dto = Dtos.newDto();
                String mmrd013[] = list.get(i).getString("MMRD013").split(",");
                List<Dto> mmrd013list = new ArrayList<>();
                if(mmrd013.length > 1){
                    for(String MMRD013: mmrd013) {
                        Dto mmrd013Dto = Dtos.newDto();
                        mmrd013Dto.put("MMRD013", MMRD013);
                        mmrd013list.add(mmrd013Dto);
                    }
                }else{
                    mmrd013list = null;
                }
                dto.put("MMRD013List", mmrd013list);
                List<Dto> result = xchbMapper.getMmrlData(dto);
                String MMRLL003 = "";
                if(result.size() > 0){
                    for(int j = 0; j < result.size(); j ++){
                        if(j == 0){
                            MMRLL003 = MMRLL003 + result.get(j).getString("MMRLL003");
                        }else{
                            MMRLL003 = MMRLL003 + "," +result.get(j).getString("MMRLL003");
                        }
                    }
                }
                list.get(i).put("MMRLL003",MMRLL003);
            }
        }
        return list;
    }

    /**
     * 现场回报保存
     *
     * @param
     * @return
     */
    @Transactional
    public void xchbSave(MmrgT mmrg_tPO, List<MmrhT> Mmrh_tPOs, List<MmriT> Mmri_tPOs, MmrdT mmrd_tPO, List<MmrqT> mmrqTList) {
        mmrg_tPO.insert();

        for(MmrhT mmrh_tPO : Mmrh_tPOs){
            mmrh_tPO.insert();
        }

        for(MmriT mmri_tPO : Mmri_tPOs){
            mmri_tPO.insert();
        }

        for(MmrqT mmrqT : mmrqTList){
            mmrqT.insert();
        }

        //修改状态
        //WHERE  mmrdent = #{mmrdent} AND mmrddocno = #{mmrddocno} AND mmrdseq = #{mmrdseq}
        UpdateWrapper<MmrdT> wrapper = new UpdateWrapper<>();
        wrapper.eq("MMRDENT",mmrd_tPO.getMMRDENT());
        wrapper.eq("MMRDDOCNO",mmrd_tPO.getMMRDDOCNO());
        wrapper.eq("MMRDSEQ",mmrd_tPO.getMMRDSEQ());
        mmrd_tPO.update(wrapper);

    }

}
